<extend name="Base/common"/>

<block name="body">
<h3>添加新的记录</h3>

  <input type="text" class="input-small" placeholder="标题" id="title">
  <input type="text" class="" placeholder="内容" id="body">

  <button type="submit" class="btn" id="add">添加</button>

<table class="table table-striped table-hover">
    <tbody>
    <tr>
        <th>编号</th>
        <th>标题</th>
        <th>内容</th>
        <th>删除</th>
    </tr>
    <notempty name="list">
        <volist name="list" id="vo">
            <tr>
                <td>{$vo.id}</td>
                <td>{$vo.title}</td>
                <td>{$vo.body}</td>
                <td><button class='btn' onclick="deletetr({$vo.id});">删除记录</button></td>
            </tr>
        </volist>
    </notempty>
    </tbody>
</table>

<script type="text/javascript">
$(document).ready(function(){
    $("#add").click(function(){
        var title = $("#title").val();
        var body  = $("#body").val();

        if(title == '' || body == ''){
            alert("信息不完整，无法添加");
            return;
        }
        $.post("{:U('addnote')}",{'title':title,'body':body},function(r){
            if(r == true){
                alert("添加成功");
            }else{
                alert("添加失败");
            }
        })
        window.location.reload();

        // var html = "<tr><td>"+num+"</td><td>"+title+"</td><td>"+body+"</td><td><button class='btn' onclick='deletetr();'>删除记录</button>";
        // $('tbody').append(html);
    })
})
function deletetr(id){
    $.post("{:U('delnote')}",{'id':id},function(r){
        if(r == true){
                alert("删除成功");
            }else{
                alert("删除失败");
            }
    })
    window.location.reload();
}
</script>
</block>